﻿/******************************************************************************
 * GTypes.cs
 * 
 * This module implements the base DataContract types needed to query Google
 * across all supported search types.
 * 
 * Date:   12/2008
 * 
 * Copyright (c) 2009, Mark Betz 
 * 
 * All rights reserved. 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer. 
 *   * Redistributions in binary form must reproduce the above copyright 
 *     notice, this list of conditions and the following disclaimer in the 
 *     documentation and/or other materials provided with the distribution. 
 *   * Neither the name of the Author nor the names of contributors may be 
 *     used to endorse or promote products derived from this software
 *     without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY MARK BETZ ''AS IS'' AND ANY  EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL MARK BETZ BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 * 
 */
using System.Runtime.Serialization;

namespace GSearch
{
    /// <summary>
    /// Implements the Google page json object, which is part of cursor
    /// </summary>
    [DataContract(Name = "pages")]
    public class GPage
    {
        /// <summary>
        /// A System.Int32 holding the zero-based index of the result item
        /// at the top of this page.
        /// </summary>
        [DataMember(Name = "start")]
        public int Start { get; set; }
        
        /// <summary>
        /// A System.String containing a display label for the page. If the
        /// page index is 0, then the display label will be '1' for example.
        /// </summary>
        [DataMember(Name = "label")]
        public string Label { get; set; }
    }

    /// <summary>
    /// Implements the Google cursor json object, which is part of responseData
    /// </summary>
    [DataContract(Name = "cursor")]
    public class GCursor
    {
        /// <summary>
        /// Returns an array of <see cref="GSearch.GPage"/> objects containing
        /// the pages available in a search result set. The system will return
        /// results in pages of either 4 or 8 items depending on the rsz querystring
        /// argument.
        /// </summary>
        [DataMember(Name = "pages")]
        public GPage[] Pages { get; set; }
        
        /// <summary>
        /// A System.String containing the total number of estimated results
        /// that the Google search system would produce for the query.
        /// </summary>
        [DataMember(Name = "estimatedResultCount")]
        public int EstimatedResultCount { get; set; }
        
        /// <summary>
        /// A System.Int32 containing the zero-based index into the Pages array
        /// where the current page can be found.
        /// </summary>
        [DataMember(Name = "currentPageIndex")]
        public int CurrentPageIndex { get; set; }
        
        /// <summary>
        /// A System.String containing a complete URL to more results from
        /// Google.
        /// </summary>
        [DataMember(Name = "moreResultsUrl")]
        public string MoreResultsUrl { get; set; }
    }

    /// <summary>
    /// Implements the common root properties of a Google result json object
    /// and names the data contract; derived classes implement search-specific
    /// results
    /// </summary>
    [DataContract(Name="results")]
    public abstract class GResults
    {
        /// <summary>
        /// A System.String containing a term describing the type of search that
        /// produced the result, i.e. 'GimageSearch'
        /// </summary>
        [DataMember(Name = "GsearchResultClass")]
        public string GSearchResultClass { get; set; }
        
        /// <summary>
        /// A System.String containing HTML content related to the search. Has
        /// always been null on return in my tests.
        /// </summary>
        [DataMember(Name = "html")]
        public string Html { get; set; }
    }

    /// <summary>
    /// Implements the root properties of the Google responseData json object and
    /// names the data contract. Derived classes implement the search-specific
    /// portions.
    /// </summary>
    [DataContract(Name="responseData")]
    public abstract class GResponseData
    {
        /// <summary>
        /// Returns the <see cref="GSearch.GCursor"/> contained in the result
        /// set.
        /// </summary>
        [DataMember(Name = "cursor")]
        public GCursor Cursor { get; set; }
    }

    /// <summary>
    /// Implements the root properties for the Google response json object and
    /// names the data contract. Derived classes implement the search-specific
    /// portions.
    /// </summary>
    [DataContract(Name="response")]
    public abstract class GResponse
    {
        /// <summary>
        /// A System.String containing text detail messages about the search
        /// </summary>
        [DataMember(Name = "responseDetails")]
        public string ResponseDetails { get; set; }
        
        /// <summary>
        /// A System.String set to the HTTP result code for the last search, i.e.
        /// '200' for a successful search.
        /// </summary>
        [DataMember(Name = "responseStatus")]
        public string ResponseStatus { get; set; }
    }

}